/*
Copyright 2011-2013 Frederic Langlet
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
you may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */

package kanzi.prediction;


public class Scan
{
    public static final byte SCAN_H  = 0; // horizontal
    public static final byte SCAN_V  = 1; // vertical
    public static final byte SCAN_Z  = 2; // zigzag
    public static final byte SCAN_HV = 3; // horizontal + vertical
       
    
    // 32x32 block left scan tables
    public static final int[][] TABLES_1024 =
    {
        // SCAN_H : horizontal
        new int[]
        {
           0,    1,    2,    3,    4,    5,    6,    7,    8,    9,   10,   11,   12,   13,   14,   15, 
          16,   17,   18,   19,   20,   21,   22,   23,   24,   25,   26,   27,   28,   29,   30,   31, 
          32,   33,   34,   35,   36,   37,   38,   39,   40,   41,   42,   43,   44,   45,   46,   47, 
          48,   49,   50,   51,   52,   53,   54,   55,   56,   57,   58,   59,   60,   61,   62,   63, 
          64,   65,   66,   67,   68,   69,   70,   71,   72,   73,   74,   75,   76,   77,   78,   79, 
          80,   81,   82,   83,   84,   85,   86,   87,   88,   89,   90,   91,   92,   93,   94,   95, 
          96,   97,   98,   99,  100,  101,  102,  103,  104,  105,  106,  107,  108,  109,  110,  111, 
         112,  113,  114,  115,  116,  117,  118,  119,  120,  121,  122,  123,  124,  125,  126,  127, 
         128,  129,  130,  131,  132,  133,  134,  135,  136,  137,  138,  139,  140,  141,  142,  143, 
         144,  145,  146,  147,  148,  149,  150,  151,  152,  153,  154,  155,  156,  157,  158,  159, 
         160,  161,  162,  163,  164,  165,  166,  167,  168,  169,  170,  171,  172,  173,  174,  175, 
         176,  177,  178,  179,  180,  181,  182,  183,  184,  185,  186,  187,  188,  189,  190,  191, 
         192,  193,  194,  195,  196,  197,  198,  199,  200,  201,  202,  203,  204,  205,  206,  207, 
         208,  209,  210,  211,  212,  213,  214,  215,  216,  217,  218,  219,  220,  221,  222,  223, 
         224,  225,  226,  227,  228,  229,  230,  231,  232,  233,  234,  235,  236,  237,  238,  239, 
         240,  241,  242,  243,  244,  245,  246,  247,  248,  249,  250,  251,  252,  253,  254,  255, 
         256,  257,  258,  259,  260,  261,  262,  263,  264,  265,  266,  267,  268,  269,  270,  271, 
         272,  273,  274,  275,  276,  277,  278,  279,  280,  281,  282,  283,  284,  285,  286,  287, 
         288,  289,  290,  291,  292,  293,  294,  295,  296,  297,  298,  299,  300,  301,  302,  303, 
         304,  305,  306,  307,  308,  309,  310,  311,  312,  313,  314,  315,  316,  317,  318,  319, 
         320,  321,  322,  323,  324,  325,  326,  327,  328,  329,  330,  331,  332,  333,  334,  335, 
         336,  337,  338,  339,  340,  341,  342,  343,  344,  345,  346,  347,  348,  349,  350,  351, 
         352,  353,  354,  355,  356,  357,  358,  359,  360,  361,  362,  363,  364,  365,  366,  367, 
         368,  369,  370,  371,  372,  373,  374,  375,  376,  377,  378,  379,  380,  381,  382,  383, 
         384,  385,  386,  387,  388,  389,  390,  391,  392,  393,  394,  395,  396,  397,  398,  399, 
         400,  401,  402,  403,  404,  405,  406,  407,  408,  409,  410,  411,  412,  413,  414,  415, 
         416,  417,  418,  419,  420,  421,  422,  423,  424,  425,  426,  427,  428,  429,  430,  431, 
         432,  433,  434,  435,  436,  437,  438,  439,  440,  441,  442,  443,  444,  445,  446,  447, 
         448,  449,  450,  451,  452,  453,  454,  455,  456,  457,  458,  459,  460,  461,  462,  463, 
         464,  465,  466,  467,  468,  469,  470,  471,  472,  473,  474,  475,  476,  477,  478,  479, 
         480,  481,  482,  483,  484,  485,  486,  487,  488,  489,  490,  491,  492,  493,  494,  495, 
         496,  497,  498,  499,  500,  501,  502,  503,  504,  505,  506,  507,  508,  509,  510,  511, 
         512,  513,  514,  515,  516,  517,  518,  519,  520,  521,  522,  523,  524,  525,  526,  527, 
         528,  529,  530,  531,  532,  533,  534,  535,  536,  537,  538,  539,  540,  541,  542,  543, 
         544,  545,  546,  547,  548,  549,  550,  551,  552,  553,  554,  555,  556,  557,  558,  559, 
         560,  561,  562,  563,  564,  565,  566,  567,  568,  569,  570,  571,  572,  573,  574,  575, 
         576,  577,  578,  579,  580,  581,  582,  583,  584,  585,  586,  587,  588,  589,  590,  591, 
         592,  593,  594,  595,  596,  597,  598,  599,  600,  601,  602,  603,  604,  605,  606,  607, 
         608,  609,  610,  611,  612,  613,  614,  615,  616,  617,  618,  619,  620,  621,  622,  623, 
         624,  625,  626,  627,  628,  629,  630,  631,  632,  633,  634,  635,  636,  637,  638,  639, 
         640,  641,  642,  643,  644,  645,  646,  647,  648,  649,  650,  651,  652,  653,  654,  655, 
         656,  657,  658,  659,  660,  661,  662,  663,  664,  665,  666,  667,  668,  669,  670,  671, 
         672,  673,  674,  675,  676,  677,  678,  679,  680,  681,  682,  683,  684,  685,  686,  687, 
         688,  689,  690,  691,  692,  693,  694,  695,  696,  697,  698,  699,  700,  701,  702,  703, 
         704,  705,  706,  707,  708,  709,  710,  711,  712,  713,  714,  715,  716,  717,  718,  719, 
         720,  721,  722,  723,  724,  725,  726,  727,  728,  729,  730,  731,  732,  733,  734,  735, 
         736,  737,  738,  739,  740,  741,  742,  743,  744,  745,  746,  747,  748,  749,  750,  751, 
         752,  753,  754,  755,  756,  757,  758,  759,  760,  761,  762,  763,  764,  765,  766,  767, 
         768,  769,  770,  771,  772,  773,  774,  775,  776,  777,  778,  779,  780,  781,  782,  783, 
         784,  785,  786,  787,  788,  789,  790,  791,  792,  793,  794,  795,  796,  797,  798,  799, 
         800,  801,  802,  803,  804,  805,  806,  807,  808,  809,  810,  811,  812,  813,  814,  815, 
         816,  817,  818,  819,  820,  821,  822,  823,  824,  825,  826,  827,  828,  829,  830,  831, 
         832,  833,  834,  835,  836,  837,  838,  839,  840,  841,  842,  843,  844,  845,  846,  847, 
         848,  849,  850,  851,  852,  853,  854,  855,  856,  857,  858,  859,  860,  861,  862,  863, 
         864,  865,  866,  867,  868,  869,  870,  871,  872,  873,  874,  875,  876,  877,  878,  879, 
         880,  881,  882,  883,  884,  885,  886,  887,  888,  889,  890,  891,  892,  893,  894,  895, 
         896,  897,  898,  899,  900,  901,  902,  903,  904,  905,  906,  907,  908,  909,  910,  911, 
         912,  913,  914,  915,  916,  917,  918,  919,  920,  921,  922,  923,  924,  925,  926,  927, 
         928,  929,  930,  931,  932,  933,  934,  935,  936,  937,  938,  939,  940,  941,  942,  943, 
         944,  945,  946,  947,  948,  949,  950,  951,  952,  953,  954,  955,  956,  957,  958,  959, 
         960,  961,  962,  963,  964,  965,  966,  967,  968,  969,  970,  971,  972,  973,  974,  975, 
         976,  977,  978,  979,  980,  981,  982,  983,  984,  985,  986,  987,  988,  989,  990,  991, 
         992,  993,  994,  995,  996,  997,  998,  999, 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 
        1008, 1009, 1010, 1011, 1012, 1013, 1014, 1015, 1016, 1017, 1018, 1019, 1020, 1021, 1022, 1023 
       },
        // SCAN_V : vertical
        new int[]
        {
           0,   32,   64,   96,  128,  160,  192,  224,  256,  288,  320,  352,  384,  416,  448,  480, 
         512,  544,  576,  608,  640,  672,  704,  736,  768,  800,  832,  864,  896,  928,  960,  992, 
           1,   33,   65,   97,  129,  161,  193,  225,  257,  289,  321,  353,  385,  417,  449,  481, 
         513,  545,  577,  609,  641,  673,  705,  737,  769,  801,  833,  865,  897,  929,  961,  993, 
           2,   34,   66,   98,  130,  162,  194,  226,  258,  290,  322,  354,  386,  418,  450,  482, 
         514,  546,  578,  610,  642,  674,  706,  738,  770,  802,  834,  866,  898,  930,  962,  994, 
           3,   35,   67,   99,  131,  163,  195,  227,  259,  291,  323,  355,  387,  419,  451,  483, 
         515,  547,  579,  611,  643,  675,  707,  739,  771,  803,  835,  867,  899,  931,  963,  995, 
           4,   36,   68,  100,  132,  164,  196,  228,  260,  292,  324,  356,  388,  420,  452,  484, 
         516,  548,  580,  612,  644,  676,  708,  740,  772,  804,  836,  868,  900,  932,  964,  996, 
           5,   37,   69,  101,  133,  165,  197,  229,  261,  293,  325,  357,  389,  421,  453,  485, 
         517,  549,  581,  613,  645,  677,  709,  741,  773,  805,  837,  869,  901,  933,  965,  997, 
           6,   38,   70,  102,  134,  166,  198,  230,  262,  294,  326,  358,  390,  422,  454,  486, 
         518,  550,  582,  614,  646,  678,  710,  742,  774,  806,  838,  870,  902,  934,  966,  998, 
           7,   39,   71,  103,  135,  167,  199,  231,  263,  295,  327,  359,  391,  423,  455,  487, 
         519,  551,  583,  615,  647,  679,  711,  743,  775,  807,  839,  871,  903,  935,  967,  999, 
           8,   40,   72,  104,  136,  168,  200,  232,  264,  296,  328,  360,  392,  424,  456,  488, 
         520,  552,  584,  616,  648,  680,  712,  744,  776,  808,  840,  872,  904,  936,  968, 1000, 
           9,   41,   73,  105,  137,  169,  201,  233,  265,  297,  329,  361,  393,  425,  457,  489, 
         521,  553,  585,  617,  649,  681,  713,  745,  777,  809,  841,  873,  905,  937,  969, 1001, 
          10,   42,   74,  106,  138,  170,  202,  234,  266,  298,  330,  362,  394,  426,  458,  490, 
         522,  554,  586,  618,  650,  682,  714,  746,  778,  810,  842,  874,  906,  938,  970, 1002, 
          11,   43,   75,  107,  139,  171,  203,  235,  267,  299,  331,  363,  395,  427,  459,  491, 
         523,  555,  587,  619,  651,  683,  715,  747,  779,  811,  843,  875,  907,  939,  971, 1003, 
          12,   44,   76,  108,  140,  172,  204,  236,  268,  300,  332,  364,  396,  428,  460,  492, 
         524,  556,  588,  620,  652,  684,  716,  748,  780,  812,  844,  876,  908,  940,  972, 1004, 
          13,   45,   77,  109,  141,  173,  205,  237,  269,  301,  333,  365,  397,  429,  461,  493, 
         525,  557,  589,  621,  653,  685,  717,  749,  781,  813,  845,  877,  909,  941,  973, 1005, 
          14,   46,   78,  110,  142,  174,  206,  238,  270,  302,  334,  366,  398,  430,  462,  494, 
         526,  558,  590,  622,  654,  686,  718,  750,  782,  814,  846,  878,  910,  942,  974, 1006, 
          15,   47,   79,  111,  143,  175,  207,  239,  271,  303,  335,  367,  399,  431,  463,  495, 
         527,  559,  591,  623,  655,  687,  719,  751,  783,  815,  847,  879,  911,  943,  975, 1007, 
          16,   48,   80,  112,  144,  176,  208,  240,  272,  304,  336,  368,  400,  432,  464,  496, 
         528,  560,  592,  624,  656,  688,  720,  752,  784,  816,  848,  880,  912,  944,  976, 1008, 
          17,   49,   81,  113,  145,  177,  209,  241,  273,  305,  337,  369,  401,  433,  465,  497, 
         529,  561,  593,  625,  657,  689,  721,  753,  785,  817,  849,  881,  913,  945,  977, 1009, 
          18,   50,   82,  114,  146,  178,  210,  242,  274,  306,  338,  370,  402,  434,  466,  498, 
         530,  562,  594,  626,  658,  690,  722,  754,  786,  818,  850,  882,  914,  946,  978, 1010, 
          19,   51,   83,  115,  147,  179,  211,  243,  275,  307,  339,  371,  403,  435,  467,  499, 
         531,  563,  595,  627,  659,  691,  723,  755,  787,  819,  851,  883,  915,  947,  979, 1011, 
          20,   52,   84,  116,  148,  180,  212,  244,  276,  308,  340,  372,  404,  436,  468,  500, 
         532,  564,  596,  628,  660,  692,  724,  756,  788,  820,  852,  884,  916,  948,  980, 1012, 
          21,   53,   85,  117,  149,  181,  213,  245,  277,  309,  341,  373,  405,  437,  469,  501, 
         533,  565,  597,  629,  661,  693,  725,  757,  789,  821,  853,  885,  917,  949,  981, 1013, 
          22,   54,   86,  118,  150,  182,  214,  246,  278,  310,  342,  374,  406,  438,  470,  502, 
         534,  566,  598,  630,  662,  694,  726,  758,  790,  822,  854,  886,  918,  950,  982, 1014, 
          23,   55,   87,  119,  151,  183,  215,  247,  279,  311,  343,  375,  407,  439,  471,  503, 
         535,  567,  599,  631,  663,  695,  727,  759,  791,  823,  855,  887,  919,  951,  983, 1015, 
          24,   56,   88,  120,  152,  184,  216,  248,  280,  312,  344,  376,  408,  440,  472,  504, 
         536,  568,  600,  632,  664,  696,  728,  760,  792,  824,  856,  888,  920,  952,  984, 1016, 
          25,   57,   89,  121,  153,  185,  217,  249,  281,  313,  345,  377,  409,  441,  473,  505, 
         537,  569,  601,  633,  665,  697,  729,  761,  793,  825,  857,  889,  921,  953,  985, 1017, 
          26,   58,   90,  122,  154,  186,  218,  250,  282,  314,  346,  378,  410,  442,  474,  506, 
         538,  570,  602,  634,  666,  698,  730,  762,  794,  826,  858,  890,  922,  954,  986, 1018, 
          27,   59,   91,  123,  155,  187,  219,  251,  283,  315,  347,  379,  411,  443,  475,  507, 
         539,  571,  603,  635,  667,  699,  731,  763,  795,  827,  859,  891,  923,  955,  987, 1019, 
          28,   60,   92,  124,  156,  188,  220,  252,  284,  316,  348,  380,  412,  444,  476,  508, 
         540,  572,  604,  636,  668,  700,  732,  764,  796,  828,  860,  892,  924,  956,  988, 1020, 
          29,   61,   93,  125,  157,  189,  221,  253,  285,  317,  349,  381,  413,  445,  477,  509, 
         541,  573,  605,  637,  669,  701,  733,  765,  797,  829,  861,  893,  925,  957,  989, 1021, 
          30,   62,   94,  126,  158,  190,  222,  254,  286,  318,  350,  382,  414,  446,  478,  510, 
         542,  574,  606,  638,  670,  702,  734,  766,  798,  830,  862,  894,  926,  958,  990, 1022, 
          31,   63,   95,  127,  159,  191,  223,  255,  287,  319,  351,  383,  415,  447,  479,  511, 
         543,  575,  607,  639,  671,  703,  735,  767,  799,  831,  863,  895,  927,  959,  991, 1023
        },
        // SCAN_Z : zigzag
        new int[]
        {
            0,    1,   32,   64,   33,    2,    3,   34,   65,   96,  128,   97,   66,   35,    4,    5, 
           36,   67,   98,  129,  160,  192,  161,  130,   99,   68,   37,    6,    7,   38,   69,  100, 
          131,  162,  193,  224,  256,  225,  194,  163,  132,  101,   70,   39,    8,    9,   40,   71, 
          102,  133,  164,  195,  226,  257,  288,  320,  289,  258,  227,  196,  165,  134,  103,   72, 
           41,   10,   11,   42,   73,  104,  135,  166,  197,  228,  259,  290,  321,  352,  384,  353, 
          322,  291,  260,  229,  198,  167,  136,  105,   74,   43,   12,   13,   44,   75,  106,  137, 
          168,  199,  230,  261,  292,  323,  354,  385,  416,  448,  417,  386,  355,  324,  293,  262, 
          231,  200,  169,  138,  107,   76,   45,   14,   15,   46,   77,  108,  139,  170,  201,  232, 
          263,  294,  325,  356,  387,  418,  449,  480,  512,  481,  450,  419,  388,  357,  326,  295, 
          264,  233,  202,  171,  140,  109,   78,   47,   16,   17,   48,   79,  110,  141,  172,  203, 
          234,  265,  296,  327,  358,  389,  420,  451,  482,  513,  544,  576,  545,  514,  483,  452, 
          421,  390,  359,  328,  297,  266,  235,  204,  173,  142,  111,   80,   49,   18,   19,   50, 
           81,  112,  143,  174,  205,  236,  267,  298,  329,  360,  391,  422,  453,  484,  515,  546, 
          577,  608,  640,  609,  578,  547,  516,  485,  454,  423,  392,  361,  330,  299,  268,  237, 
          206,  175,  144,  113,   82,   51,   20,   21,   52,   83,  114,  145,  176,  207,  238,  269, 
          300,  331,  362,  393,  424,  455,  486,  517,  548,  579,  610,  641,  672,  704,  673,  642, 
          611,  580,  549,  518,  487,  456,  425,  394,  363,  332,  301,  270,  239,  208,  177,  146, 
          115,   84,   53,   22,   23,   54,   85,  116,  147,  178,  209,  240,  271,  302,  333,  364, 
          395,  426,  457,  488,  519,  550,  581,  612,  643,  674,  705,  736,  768,  737,  706,  675, 
          644,  613,  582,  551,  520,  489,  458,  427,  396,  365,  334,  303,  272,  241,  210,  179, 
          148,  117,   86,   55,   24,   25,   56,   87,  118,  149,  180,  211,  242,  273,  304,  335, 
          366,  397,  428,  459,  490,  521,  552,  583,  614,  645,  676,  707,  738,  769,  800,  832, 
          801,  770,  739,  708,  677,  646,  615,  584,  553,  522,  491,  460,  429,  398,  367,  336, 
          305,  274,  243,  212,  181,  150,  119,   88,   57,   26,   27,   58,   89,  120,  151,  182, 
          213,  244,  275,  306,  337,  368,  399,  430,  461,  492,  523,  554,  585,  616,  647,  678, 
          709,  740,  771,  802,  833,  864,  896,  865,  834,  803,  772,  741,  710,  679,  648,  617, 
          586,  555,  524,  493,  462,  431,  400,  369,  338,  307,  276,  245,  214,  183,  152,  121, 
           90,   59,   28,   29,   60,   91,  122,  153,  184,  215,  246,  277,  308,  339,  370,  401, 
          432,  463,  494,  525,  556,  587,  618,  649,  680,  711,  742,  773,  804,  835,  866,  897, 
          928,  960,  929,  898,  867,  836,  805,  774,  743,  712,  681,  650,  619,  588,  557,  526, 
          495,  464,  433,  402,  371,  340,  309,  278,  247,  216,  185,  154,  123,   92,   61,   30, 
           31,   62,   93,  124,  155,  186,  217,  248,  279,  310,  341,  372,  403,  434,  465,  496, 
          527,  558,  589,  620,  651,  682,  713,  744,  775,  806,  837,  868,  899,  930,  961,  992, 
          993,  962,  931,  900,  869,  838,  807,  776,  745,  714,  683,  652,  621,  590,  559,  528, 
          497,  466,  435,  404,  373,  342,  311,  280,  249,  218,  187,  156,  125,   94,   63,   95, 
          126,  157,  188,  219,  250,  281,  312,  343,  374,  405,  436,  467,  498,  529,  560,  591, 
          622,  653,  684,  715,  746,  777,  808,  839,  870,  901,  932,  963,  994,  995,  964,  933, 
          902,  871,  840,  809,  778,  747,  716,  685,  654,  623,  592,  561,  530,  499,  468,  437, 
          406,  375,  344,  313,  282,  251,  220,  189,  158,  127,  159,  190,  221,  252,  283,  314, 
          345,  376,  407,  438,  469,  500,  531,  562,  593,  624,  655,  686,  717,  748,  779,  810, 
          841,  872,  903,  934,  965,  996,  997,  966,  935,  904,  873,  842,  811,  780,  749,  718, 
          687,  656,  625,  594,  563,  532,  501,  470,  439,  408,  377,  346,  315,  284,  253,  222, 
          191,  223,  254,  285,  316,  347,  378,  409,  440,  471,  502,  533,  564,  595,  626,  657, 
          688,  719,  750,  781,  812,  843,  874,  905,  936,  967,  998,  999,  968,  937,  906,  875, 
          844,  813,  782,  751,  720,  689,  658,  627,  596,  565,  534,  503,  472,  441,  410,  379, 
          348,  317,  286,  255,  287,  318,  349,  380,  411,  442,  473,  504,  535,  566,  597,  628, 
          659,  690,  721,  752,  783,  814,  845,  876,  907,  938,  969, 1000, 1001,  970,  939,  908, 
          877,  846,  815,  784,  753,  722,  691,  660,  629,  598,  567,  536,  505,  474,  443,  412, 
          381,  350,  319,  351,  382,  413,  444,  475,  506,  537,  568,  599,  630,  661,  692,  723, 
          754,  785,  816,  847,  878,  909,  940,  971, 1002, 1003,  972,  941,  910,  879,  848,  817, 
          786,  755,  724,  693,  662,  631,  600,  569,  538,  507,  476,  445,  414,  383,  415,  446, 
          477,  508,  539,  570,  601,  632,  663,  694,  725,  756,  787,  818,  849,  880,  911,  942, 
          973, 1004, 1005,  974,  943,  912,  881,  850,  819,  788,  757,  726,  695,  664,  633,  602, 
          571,  540,  509,  478,  447,  479,  510,  541,  572,  603,  634,  665,  696,  727,  758,  789, 
          820,  851,  882,  913,  944,  975, 1006, 1007,  976,  945,  914,  883,  852,  821,  790,  759, 
          728,  697,  666,  635,  604,  573,  542,  511,  543,  574,  605,  636,  667,  698,  729,  760, 
          791,  822,  853,  884,  915,  946,  977, 1008, 1009,  978,  947,  916,  885,  854,  823,  792, 
          761,  730,  699,  668,  637,  606,  575,  607,  638,  669,  700,  731,  762,  793,  824,  855, 
          886,  917,  948,  979, 1010, 1011,  980,  949,  918,  887,  856,  825,  794,  763,  732,  701, 
          670,  639,  671,  702,  733,  764,  795,  826,  857,  888,  919,  950,  981, 1012, 1013,  982, 
          951,  920,  889,  858,  827,  796,  765,  734,  703,  735,  766,  797,  828,  859,  890,  921, 
          952,  983, 1014, 1015,  984,  953,  922,  891,  860,  829,  798,  767,  799,  830,  861,  892, 
          923,  954,  985, 1016, 1017,  986,  955,  924,  893,  862,  831,  863,  894,  925,  956,  987, 
         1018, 1019,  988,  957,  926,  895,  927,  958,  989, 1020, 1021,  990,  959,  991, 1022, 1023
        },
        // SCAN_HV : mix horizontal + vertical
        new int[]
        {
            0,    1,   32,    2,   64,    3,   96,    4,  128,    5,  160,    6,  192,    7,  224,    8, 
          256,    9,  288,   10,  320,   11,  352,   12,  384,   13,  416,   14,  448,   15,  480,   16, 
          512,   17,  544,   18,  576,   19,  608,   20,  640,   21,  672,   22,  704,   23,  736,   24, 
          768,   25,  800,   26,  832,   27,  864,   28,  896,   29,  928,   30,  960,   31,  992,   33, 
           34,   65,   35,   97,   36,  129,   37,  161,   38,  193,   39,  225,   40,  257,   41,  289, 
           42,  321,   43,  353,   44,  385,   45,  417,   46,  449,   47,  481,   48,  513,   49,  545, 
           50,  577,   51,  609,   52,  641,   53,  673,   54,  705,   55,  737,   56,  769,   57,  801, 
           58,  833,   59,  865,   60,  897,   61,  929,   62,  961,   63,  993,   66,   67,   98,   68, 
          130,   69,  162,   70,  194,   71,  226,   72,  258,   73,  290,   74,  322,   75,  354,   76, 
          386,   77,  418,   78,  450,   79,  482,   80,  514,   81,  546,   82,  578,   83,  610,   84, 
          642,   85,  674,   86,  706,   87,  738,   88,  770,   89,  802,   90,  834,   91,  866,   92, 
          898,   93,  930,   94,  962,   95,  994,   99,  100,  131,  101,  163,  102,  195,  103,  227, 
          104,  259,  105,  291,  106,  323,  107,  355,  108,  387,  109,  419,  110,  451,  111,  483, 
          112,  515,  113,  547,  114,  579,  115,  611,  116,  643,  117,  675,  118,  707,  119,  739, 
          120,  771,  121,  803,  122,  835,  123,  867,  124,  899,  125,  931,  126,  963,  127,  995, 
          132,  133,  164,  134,  196,  135,  228,  136,  260,  137,  292,  138,  324,  139,  356,  140, 
          388,  141,  420,  142,  452,  143,  484,  144,  516,  145,  548,  146,  580,  147,  612,  148, 
          644,  149,  676,  150,  708,  151,  740,  152,  772,  153,  804,  154,  836,  155,  868,  156, 
          900,  157,  932,  158,  964,  159,  996,  165,  166,  197,  167,  229,  168,  261,  169,  293, 
          170,  325,  171,  357,  172,  389,  173,  421,  174,  453,  175,  485,  176,  517,  177,  549, 
          178,  581,  179,  613,  180,  645,  181,  677,  182,  709,  183,  741,  184,  773,  185,  805, 
          186,  837,  187,  869,  188,  901,  189,  933,  190,  965,  191,  997,  198,  199,  230,  200, 
          262,  201,  294,  202,  326,  203,  358,  204,  390,  205,  422,  206,  454,  207,  486,  208, 
          518,  209,  550,  210,  582,  211,  614,  212,  646,  213,  678,  214,  710,  215,  742,  216, 
          774,  217,  806,  218,  838,  219,  870,  220,  902,  221,  934,  222,  966,  223,  998,  231, 
          232,  263,  233,  295,  234,  327,  235,  359,  236,  391,  237,  423,  238,  455,  239,  487, 
          240,  519,  241,  551,  242,  583,  243,  615,  244,  647,  245,  679,  246,  711,  247,  743, 
          248,  775,  249,  807,  250,  839,  251,  871,  252,  903,  253,  935,  254,  967,  255,  999, 
          264,  265,  296,  266,  328,  267,  360,  268,  392,  269,  424,  270,  456,  271,  488,  272, 
          520,  273,  552,  274,  584,  275,  616,  276,  648,  277,  680,  278,  712,  279,  744,  280, 
          776,  281,  808,  282,  840,  283,  872,  284,  904,  285,  936,  286,  968,  287, 1000,  297, 
          298,  329,  299,  361,  300,  393,  301,  425,  302,  457,  303,  489,  304,  521,  305,  553, 
          306,  585,  307,  617,  308,  649,  309,  681,  310,  713,  311,  745,  312,  777,  313,  809, 
          314,  841,  315,  873,  316,  905,  317,  937,  318,  969,  319, 1001,  330,  331,  362,  332, 
          394,  333,  426,  334,  458,  335,  490,  336,  522,  337,  554,  338,  586,  339,  618,  340, 
          650,  341,  682,  342,  714,  343,  746,  344,  778,  345,  810,  346,  842,  347,  874,  348, 
          906,  349,  938,  350,  970,  351, 1002,  363,  364,  395,  365,  427,  366,  459,  367,  491, 
          368,  523,  369,  555,  370,  587,  371,  619,  372,  651,  373,  683,  374,  715,  375,  747, 
          376,  779,  377,  811,  378,  843,  379,  875,  380,  907,  381,  939,  382,  971,  383, 1003, 
          396,  397,  428,  398,  460,  399,  492,  400,  524,  401,  556,  402,  588,  403,  620,  404, 
          652,  405,  684,  406,  716,  407,  748,  408,  780,  409,  812,  410,  844,  411,  876,  412, 
          908,  413,  940,  414,  972,  415, 1004,  429,  430,  461,  431,  493,  432,  525,  433,  557, 
          434,  589,  435,  621,  436,  653,  437,  685,  438,  717,  439,  749,  440,  781,  441,  813, 
          442,  845,  443,  877,  444,  909,  445,  941,  446,  973,  447, 1005,  462,  463,  494,  464, 
          526,  465,  558,  466,  590,  467,  622,  468,  654,  469,  686,  470,  718,  471,  750,  472, 
          782,  473,  814,  474,  846,  475,  878,  476,  910,  477,  942,  478,  974,  479, 1006,  495, 
          496,  527,  497,  559,  498,  591,  499,  623,  500,  655,  501,  687,  502,  719,  503,  751, 
          504,  783,  505,  815,  506,  847,  507,  879,  508,  911,  509,  943,  510,  975,  511, 1007, 
          528,  529,  560,  530,  592,  531,  624,  532,  656,  533,  688,  534,  720,  535,  752,  536, 
          784,  537,  816,  538,  848,  539,  880,  540,  912,  541,  944,  542,  976,  543, 1008,  561, 
          562,  593,  563,  625,  564,  657,  565,  689,  566,  721,  567,  753,  568,  785,  569,  817, 
          570,  849,  571,  881,  572,  913,  573,  945,  574,  977,  575, 1009,  594,  595,  626,  596, 
          658,  597,  690,  598,  722,  599,  754,  600,  786,  601,  818,  602,  850,  603,  882,  604, 
          914,  605,  946,  606,  978,  607, 1010,  627,  628,  659,  629,  691,  630,  723,  631,  755, 
          632,  787,  633,  819,  634,  851,  635,  883,  636,  915,  637,  947,  638,  979,  639, 1011, 
          660,  661,  692,  662,  724,  663,  756,  664,  788,  665,  820,  666,  852,  667,  884,  668, 
          916,  669,  948,  670,  980,  671, 1012,  693,  694,  725,  695,  757,  696,  789,  697,  821, 
          698,  853,  699,  885,  700,  917,  701,  949,  702,  981,  703, 1013,  726,  727,  758,  728, 
          790,  729,  822,  730,  854,  731,  886,  732,  918,  733,  950,  734,  982,  735, 1014,  759, 
          760,  791,  761,  823,  762,  855,  763,  887,  764,  919,  765,  951,  766,  983,  767, 1015, 
          792,  793,  824,  794,  856,  795,  888,  796,  920,  797,  952,  798,  984,  799, 1016,  825, 
          826,  857,  827,  889,  828,  921,  829,  953,  830,  985,  831, 1017,  858,  859,  890,  860, 
          922,  861,  954,  862,  986,  863, 1018,  891,  892,  923,  893,  955,  894,  987,  895, 1019, 
          924,  925,  956,  926,  988,  927, 1020,  957,  958,  989,  959, 1021,  990,  991, 1022, 1023
        }
    };
    
    
    // 16x16 block left scan tables
    public static final int[][] TABLES_256 =
    {
        // SCAN_H : horizontal
        new int[]
        {    0,   1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,  13,  14,  15,
            16,  17,  18,  19,  20,  21,  22,  23,  24,  25,  26,  27,  28,  29,  30,  31,
            32,  33,  34,  35,  36,  37,  38,  39,  40,  41,  42,  43,  44,  45,  46,  47,
            48,  49,  50,  51,  52,  53,  54,  55,  56,  57,  58,  59,  60,  61,  62,  63,
            64,  65,  66,  67,  68,  69,  70,  71,  72,  73,  74,  75,  76,  77,  78,  79,
            80,  81,  82,  83,  84,  85,  86,  87,  88,  89,  90,  91,  92,  93,  94,  95,
            96,  97,  98,  99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
           112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
           128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
           144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
           160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
           176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
           192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
           208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223,
           224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239,
           240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255
        },
        // SCAN_V : vertical
        new int[]
        {    0,  16,  32,  48,  64,  80,  96, 112, 128, 144, 160, 176, 192, 208, 224, 240,
             1,  17,  33,  49,  65,  81,  97, 113, 129, 145, 161, 177, 193, 209, 225, 241,
             2,  18,  34,  50,  66,  82,  98, 114, 130, 146, 162, 178, 194, 210, 226, 242,
             3,  19,  35,  51,  67,  83,  99, 115, 131, 147, 163, 179, 195, 211, 227, 243,
             4,  20,  36,  52,  68,  84, 100, 116, 132, 148, 164, 180, 196, 212, 228, 244,
             5,  21,  37,  53,  69,  85, 101, 117, 133, 149, 165, 181, 197, 213, 229, 245,
             6,  22,  38,  54,  70,  86, 102, 118, 134, 150, 166, 182, 198, 214, 230, 246,
             7,  23,  39,  55,  71,  87, 103, 119, 135, 151, 167, 183, 199, 215, 231, 247,
             8,  24,  40,  56,  72,  88, 104, 120, 136, 152, 168, 184, 200, 216, 232, 248,
             9,  25,  41,  57,  73,  89, 105, 121, 137, 153, 169, 185, 201, 217, 233, 249,
            10,  26,  42,  58,  74,  90, 106, 122, 138, 154, 170, 186, 202, 218, 234, 250,
            11,  27,  43,  59,  75,  91, 107, 123, 139, 155, 171, 187, 203, 219, 235, 251,
            12,  28,  44,  60,  76,  92, 108, 124, 140, 156, 172, 188, 204, 220, 236, 252,
            13,  29,  45,  61,  77,  93, 109, 125, 141, 157, 173, 189, 205, 221, 237, 253,
            14,  30,  46,  62,  78,  94, 110, 126, 142, 158, 174, 190, 206, 222, 238, 254,
            15,  31,  47,  63,  79,  95, 111, 127, 143, 159, 175, 191, 207, 223, 239, 255
        },
        // SCAN_Z : zigzag
        new int[]
        {    0,   1,  16,  32,  17,   2,   3,  18,  33,  48,  64,  49,  34,  19,   4,   5,
            20,  35,  50,  65,  80,  96,  81,  66,  51,  36,  21,   6,   7,  22,  37,  52,
            67,  82,  97, 112, 128, 113,  98,  83,  68,  53,  38,  23,   8,   9,  24,  39,
            54,  69,  84,  99, 114, 129, 144, 160, 145, 130, 115, 100,  85,  70,  55,  40,
            25,  10,  11,  26,  41,  56,  71,  86, 101, 116, 131, 146, 161, 176, 192, 177,
           162, 147, 132, 117, 102,  87,  72,  57,  42,  27,  12,  13,  28,  43,  58,  73,
            88, 103, 118, 133, 148, 163, 178, 193, 208, 224, 209, 194, 179, 164, 149, 134,
           119, 104,  89,  74,  59,  44,  29,  14,  15,  30,  45,  60,  75,  90, 105, 120,
           135, 150, 165, 180, 195, 210, 225, 240, 241, 226, 211, 196, 181, 166, 151, 136,
           121, 106, 91,  76,   61,  46,  31,  47,  62,  77,  92, 107, 122, 137, 152, 167,
           182, 197, 212, 227, 242, 243, 228, 213, 198, 183, 168, 153, 138, 123, 108,  93,
            78,  63,  79,  94, 109, 124, 139, 154, 169, 184, 199, 214, 229, 244, 245, 230,
           215, 200, 185, 170, 155, 140, 125, 110,  95, 111, 126, 141, 156, 171, 186, 201,
           216, 231, 246, 247, 232, 217, 202, 187, 172, 157, 142, 127, 143, 158, 173, 188,
           203, 218, 233, 248, 249, 234, 219, 204, 189, 174, 159, 175, 190, 205, 220, 235,
           250, 251, 236, 221, 206, 191, 207, 222, 237, 252, 253, 238, 223, 239, 254, 255
        },
        // SCAN_HV : mix horizontal + vertical
        new int[]
        {    0,   1,  16,   2,  32,   3,  48,   4,  64,   5,  80,   6,  96,   7, 112,   8,
           128,   9, 144,  10, 160,  11, 176,  12, 192,  13, 208,  14, 224,  15, 240,  17,
            18,  33,  19,  49,  20,  65,  21,  81,  22,  97,  23, 113,  24, 129,  25, 145,
            26, 161,  27, 177,  28, 193,  29, 209,  30, 225,  31, 241,  34,  35,  50,  36,
            66,  37,  82,  38,  98,  39, 114,  40, 130,  41, 146,  42, 162,  43, 178,  44,
           194,  45, 210,  46, 226,  47, 242,  51,  52,  67,  53,  83,  54,  99,  55, 115,
            56, 131,  57, 147,  58, 163,  59, 179,  60, 195,  61, 211,  62, 227,  63, 243,
            68,  69,  84,  70, 100,  71, 116,  72, 132,  73, 148,  74, 164,  75, 180,  76,
           196,  77, 212,  78, 228,  79, 244,  85,  86, 101,  87, 117,  88, 133,  89, 149,
            90, 165,  91, 181,  92, 197,  93,  213, 94, 229,  95, 245, 102, 103, 118, 104,
           134, 105, 150, 106, 166, 107, 182, 108, 198, 109, 214, 110, 230, 111, 246, 119,
           120, 135, 121, 151, 122, 167, 123, 183, 124, 199, 125, 215, 126, 231, 127, 247,
           136, 137, 152, 138, 168, 139, 184, 140, 200, 141, 216, 142, 232, 143, 248, 153,
           154, 169, 155, 185, 156, 201, 157, 217, 158, 233, 159, 249, 170, 171, 186, 172,
           202, 173, 218, 174, 234, 175, 250, 187, 188, 203, 189, 219, 190, 235, 191, 251,
           204, 205, 220, 206, 236, 207, 252, 221, 222, 237, 223, 253, 238, 239, 254, 255
        }
    };

    // 8x8 block left scan tables
    public static final int[][] TABLES_64 =
    {
        // SCAN_H : horizontal
        new int[]
        {  0,  1,  2,  3,  4,  5,  6,  7,
           8,  9, 10, 11, 12, 13, 14, 15,
          16, 17, 18, 19, 20, 21, 22, 23,
          24, 25, 26, 27, 28, 29, 30, 31,
          32, 33, 34, 35, 36, 37, 38, 39,
          40, 41, 42, 43, 44, 45, 46, 47,
          48, 49, 50, 51, 52, 53, 54, 55,
          56, 57, 58, 59, 60, 61, 62, 63
        },
        // SCAN_V : vertical
        new int[]
        {  0,  8, 16, 24, 32, 40, 48, 56,
           1,  9, 17, 25, 33, 41, 49, 57,
           2, 10, 18, 26, 34, 42, 50, 58,
           3, 11, 19, 27, 35, 43, 51, 59,
           4, 12, 20, 28, 36, 44, 52, 60,
           5, 13, 21, 29, 37, 45, 53, 61,
           6, 14, 22, 30, 38, 46, 54, 62,
           7, 15, 23, 31, 39, 47, 55, 63
        },
        // SCAN_Z : zigzag
        new int[]
        {  0,  1,  8, 16,  9,  2,  3, 10,
          17, 24, 32, 25, 18, 11,  4,  5,
          12, 19, 26, 33, 40, 48, 41, 34,
          27, 20, 13,  6,  7, 14, 21, 28,
          35, 42, 49, 56, 57, 50, 43, 36,
          29, 22, 15, 23, 30, 37, 44, 51,
          58, 59, 52, 45, 38, 31, 39, 46,
          53, 60, 61, 54, 47, 55, 62, 63
        },
        // SCAN_HV : mix horizontal + vertical
        new int[]
        {  0,  1,  8,  2, 16,  3, 24,  4,
          32,  5, 40,  6, 48,  7, 56,  9,
          10, 17, 11, 25, 12, 33, 13, 41,
          14, 49, 15, 57, 18, 19, 26, 20,
          34, 21, 42, 22, 50, 23, 58, 27,
          28, 35, 29, 43, 30, 51, 31, 59,
          36, 37, 44, 38, 52, 39, 60, 45,
          46, 53, 47, 61, 54, 55, 62, 63
        }
    };


    // 4x4 block left scan tables
    public static final int[][] TABLES_16 =
    {
        // SCAN_H : horizontal
        new int[]
        {  0,  1,  2,  3,
           4,  5,  6,  7,
           8,  9, 10, 11,
          12, 13, 14, 15
        },
        // SCAN_V : vertical
        new int[]
        {  0,  4,  8, 12,
           1,  5,  9, 13,
           2,  6, 10, 14,
           3,  7, 11, 15
        },
        // SCAN_Z : zigzag
        new int[]
        {  0,  1,  4,  8,
           5,  2,  3,  6,
           9, 12, 13, 10,
           7, 11, 14, 15
        },
        // SCAN_HV : mix horizontal + vertical
        new int[]
        {  0,  1,  4,  2,
           8,  3, 12,  5,
           6,  9,  7, 13,
          10, 11, 14, 15
        }
    };
}

